package org.dream.ui.chart.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

import org.dream.domain.Daydeal;
import org.dream.ui.chart.Chart;
import org.dream.ui.chart.Plot;
import org.dream.ui.chart.RectTip;

/**
 * 价格K线
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
class KPlot extends Plot {
	private List<Daydeal> daydealList;

	public KPlot(List<Daydeal> daydealList, Chart chart, int index) {
		super(chart, index);
		this.daydealList = daydealList;
		title = daydealList.get(0).getName() + "(日线)";

	}

	@Override
	public Double getMaxY() {
		Double maxY = -Double.MAX_VALUE;
		for (int i = MinX; i <= maxX && i < daydealList.size(); i++) {
			Daydeal daydeal = null;
			try {
				daydeal = daydealList.get(i);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (daydeal.getHIGH().compareTo(maxY) > 0) {
				maxY = daydeal.getHIGH();
			}
		}
		return maxY;
	}

	@Override
	public Double getMinY() {
		Double minY = Double.MAX_VALUE;
		for (int i = MinX; i <= maxX && i < daydealList.size(); i++) {
			Daydeal daydeal = daydealList.get(i);
			if (daydeal.getLOW().compareTo(minY) < 0) {
				minY = daydeal.getLOW();
			}
		}
		return minY;
	}

	@Override
	public void draw(Graphics g) {

		for (int i = MinX; i <= maxX; i++) {
			Daydeal daydeal = daydealList.get(i);
			int dayXPixel = chart.getXCoordinatePixel(new Double(i + 1));
			int OPEN = chart.getYCoordinatePixel(daydeal.getOPEN(), index);
			int CLOSE = chart.getYCoordinatePixel(daydeal.getCLOSE(), index);
			int HIGH = chart.getYCoordinatePixel(daydeal.getHIGH(), index);
			int LOW = chart.getYCoordinatePixel(daydeal.getLOW(), index);
			// 58:18
			int halfWidth = (int) (chart.getXAxis().getXAxisModel().getTickSpacingPixel() / (58 + 18) * 58 / 2);
			String tipStr = daydeal.getDAY() + ":开盘价格：" + daydeal.getOPEN() + ":收盘价格：" + daydeal.getCLOSE()
					+ ":涨幅：" + daydeal.getRiseWidth();
			if (daydeal.getOPEN() < daydeal.getCLOSE()) {
				g.setColor(Color.RED);
				g.drawRect(dayXPixel - halfWidth, CLOSE, halfWidth * 2, Math.abs(CLOSE - OPEN));
				// g.fillRect(dayXPixel - halfWidth, CLOSE, halfWidth * 2,
				// Math.abs(CLOSE - OPEN));
				g.drawLine(dayXPixel, LOW, dayXPixel, OPEN);
				g.drawLine(dayXPixel, CLOSE, dayXPixel, HIGH);
			} else if (daydeal.getOPEN() > daydeal.getCLOSE()) {
				g.setColor(new Color(84, 255, 255));
				g.drawRect(dayXPixel - halfWidth, OPEN, halfWidth * 2, Math.abs(OPEN - CLOSE));
				g.fillRect(dayXPixel - halfWidth, OPEN, halfWidth * 2, Math.abs(OPEN - CLOSE));
				g.drawLine(dayXPixel, LOW, dayXPixel, CLOSE);
				g.drawLine(dayXPixel, OPEN, dayXPixel, HIGH);
				// 添加区域事件
				RectTip rectTip = new RectTip(dayXPixel - halfWidth, OPEN, dayXPixel + halfWidth, CLOSE,
						tipStr);
				((KChart) chart).addRectTip(rectTip);
			} else {
				g.setColor(Color.WHITE);
				g.drawRect(dayXPixel - halfWidth, CLOSE, halfWidth * 2, Math.abs(CLOSE - OPEN));
				g.drawLine(dayXPixel, LOW, dayXPixel, OPEN);
				g.drawLine(dayXPixel, CLOSE, dayXPixel, HIGH);
				// 添加区域事件

				RectTip rectTip = new RectTip(dayXPixel - halfWidth, CLOSE, dayXPixel + halfWidth, OPEN,
						tipStr);
				((KChart) chart).addRectTip(rectTip);

			}

			// 画S
			if (Math.abs(daydeal.getRiseWidth()) > 20d) {
				g.drawString("s", dayXPixel, chart.getYAxis(index).getYAxisModel().getPixelYButtom());
			}

		}
		g.drawString(title, chart.getYAxis(index).getYAxisModel().getPixelXLeft(), chart.getYAxis(index)
				.getYAxisModel().getPixelYTop());

	}

}
